import java.net.URLEncoder;
import java.net.URLDecoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.io.StringReader;


public class SecurityExample
{
	static SecretKeySpec keySpec;
	
	public static String encryptString(String str, String key) throws Exception 
	{
	
	  byte[] keyBytes = key.getBytes("UTF-8");
	  SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "DESede");
	  Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
	  cipher.init(Cipher.ENCRYPT_MODE, keySpec);
	  byte[] clrtxt = str.getBytes("UTF8");
	  byte[] cphtxt = cipher.doFinal(clrtxt);
	  return new String(cphtxt, "UTF-8");
	  //BASE64Encoder base64encoder = new BASE64Encoder();
	  //return URLEncoder.encode(base64encoder.encode(cphtxt), "UTF-8");
	
	}
	
	public static String decryptString(String str, String key) throws Exception 
	{
	  
	  //str = URLDecoder.decode(str, "UTF-8"); 
	  byte[] keyBytes = key.getBytes("UTF-8");
	  keySpec = new SecretKeySpec(keyBytes, "DESede");
	  Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
	  cipher.init(Cipher.DECRYPT_MODE, keySpec);
	  //BASE64Decoder base64decoder = new BASE64Decoder();
	  //byte[] clrtxt = base64decoder.decodeBuffer(str);
	  byte[] clrtxt = str.getBytes();
	  byte[] cphtxt = cipher.doFinal(clrtxt);
	  StringBuffer sBuffer = new StringBuffer();
	  for (int i = 0; i < cphtxt.length; i++) 
	  {
	    sBuffer.append((char) cphtxt[i]);
	  }
	  return sBuffer.toString();
	}
	
	private static String getEncryptionKey() throws Exception
	{
		return "U9BX16YTZ7LQJ5F3S6HW0I0X";
	}
	
	public static void main (String [] args) throws Exception
	{
	   String originalValue = args[0];
	   System.out.println("originalValue:\n [" + originalValue + "]");
	   String encryptedValue = encryptString(originalValue, getEncryptionKey());
	   System.out.println("encryptedValue:\n [" + encryptedValue + "]");
	   String decryptedValue = decryptString(encryptedValue, getEncryptionKey());
	   System.out.println("decryptedValue:\n [" + decryptedValue + "]");
	   
	}  
	
	
	
}